//
//  UIView+Extension.m
//  01-黑酷
//
//  Created by apple on 14-6-27.
//  Copyright (c) 2014年 heima. All rights reserved.
//

#import "UIView+Extension.h"

@implementation UIView (Extension)

- (void)setX:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (CGFloat)x
{
    return self.frame.origin.x;
}

- (void)setY:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)y
{
    return self.frame.origin.y;
}

- (void)setWidth:(CGFloat)width
{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (void)setCenterX:(CGFloat)centerX
{
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}

- (CGFloat)centerX
{
    return self.center.x;
}

- (void)setCenterY:(CGFloat)centerY
{
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}

- (CGFloat)centerY
{
    return self.center.y;
}

- (CGFloat)width
{
    return self.frame.size.width;
}

- (void)setHeight:(CGFloat)height
{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)height
{
    return self.frame.size.height;
}

- (void)setSize:(CGSize)size
{
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (CGSize)size
{
    return self.frame.size;
}

static CGFloat height = 0.5;
//头尾描边
+ (UIView *)strokeView:(UIView*)view header:(BOOL)header footer:(BOOL)footer
{
    //头部描边
    if (header) {
        UIView *headerStroke = [[UIView alloc] initWithFrame:CGRectMake(0, 0, view.bounds.size.width, height)];
        headerStroke.backgroundColor = UIColorFromRGB(0xe8eae9);
        headerStroke.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
        [view addSubview:headerStroke];
    }
    //尾部描边
    if (footer) {
        UIView *footerStroke = [[UIView alloc] initWithFrame:CGRectMake(0, view.bounds.size.height - height, view.bounds.size.width, height)];
        footerStroke.backgroundColor = UIColorFromRGB(0xe8eae9);
        footerStroke.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
        [view addSubview:footerStroke];
    }
    return view;
}

//按比例增加描边
+ (UIView *)strokeView:(UIView *)view ratio:(NSInteger)ratio
{
    for (int i=0; i <=ratio; i++) {
        CGFloat y = 0;
        if (i == 0) {
            y = 0;
        }else if (i == ratio)
        {
            y = view.height - height;
        }else
        {
            y = view.height / ratio * i;
        }
        UIView *headerStroke = [[UIView alloc] initWithFrame:CGRectMake(0, y, view.bounds.size.width, height)];
        headerStroke.backgroundColor = UIColorFromRGB(0xe8eae9);
        headerStroke.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
        [view addSubview:headerStroke];
    }
    return view;
}

#pragma mark --计算字符空间大小
+ (CGSize)sizeWithString:(NSString *)string font:(UIFont *)font width:(NSInteger)width
{
    CGRect rect = [string boundingRectWithSize:CGSizeMake(width, 1000)//显示的最大容量
                                       options: NSStringDrawingUsesLineFragmentOrigin //描述字符串的附加参数
                                    attributes:@{NSFontAttributeName: font}//描述字符串的参数
                                       context:nil];//上下文
                                                    //返回值
    return rect.size;
}




+ (CGSize)sizeWithString:(NSString *)string font:(UIFont *)font width:(NSInteger)width height:(NSInteger)height
{
    CGRect rect = [string boundingRectWithSize:CGSizeMake(width, height)//显示的最大容量
                                       options: NSStringDrawingUsesLineFragmentOrigin //描述字符串的附加参数
                                    attributes:@{NSFontAttributeName: font}//描述字符串的参数
                                       context:nil];//上下文
                                                    //返回值
    return rect.size;
}

+ (void)showAlter:(NSString *)message
{
    UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alter show];
}

@end
